<?php 
$weekarr = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$week = $weekarr[date("w")];
?>
<div id="order" class="indexcontentdiv">
    <div class="positiondiv">
        <table>
            <tr>
                <td class="td-left">
                    <img src="<?=base_url()?>resource/img/home_ico.png" width="21" height="17">
                    Data Entry > <a href="<?=base_url()?>profile/order">Order</a> > New Order
                </td>
                <td class="td-right">
                    <img src="<?=base_url()?>resource/img/menu2.png" width="27" height="24">Today is <?=$week?>,<?=date('M d,Y')?>
                </td>
            </tr>
        </table>
    </div>
    <div class="input-div">
        <form action="<?=base_url('profile/order_detail')?>/<?=$order['id']?>" method="post" onkeydown="if(event.keyCode==13)return false;" onsubmit="return checkform()">
            <input type="hidden" value="<?=$order['id']?>" name="id"/>
        <table>
            <tr><td colspan="2"><h3>.Order</h3></td></tr>
            <tr><td class="nametd">Order No:</td><td><input type="text" name="sn" value="<?=$order['sn']?>"/><span id="snmsg" class="err">*</span></td></tr>
            <tr>
                <td class="nametd">Company Name:</td>
                <td>
                    <input type="text" name="name" id="name" readonly="readonly" title="" value="<?=$order['companyname']?>"/>
                    <input type="hidden" name="companyid" id="companyid" value="<?=$order['companyid']?>"/>
                </td>
            </tr>
            <tr><td class="nametd">Position:</td><td><input type="text" name="position" value="<?=$order['position']?>"/><span id="positionmsg" class="err">*</span></td></tr>
            <tr><td class="nametd">Salary:</td><td><input type="text" name="salary" value="<?=$order['salary']?>"/><span id="salarymsg" class="err">*</span></td></tr>
            <tr>
                <td class="nametd">Gender:</td>
                <td>
                    <select name="gender" id="gender" style="width:120px">
                        <option value="0">Male or Female
                        <option value="1">Male
                        <option value="2">Female
                    </select>
                </td>
            </tr>
            <tr><td class="nametd">Work Intro:</td>
                <td>
                    <textarea name="workintro"><?=$order['workintro']?></textarea>
                </td>
            </tr>
            <tr>
                <td class="nametd">Priority:</td>
                <td>
                    <select name="priority" id="priority" style="width:120px;">
                        <option value="0">Other
                        <option value="1">A
                        <option value="2">B
                        <option value="3">C
                    </select>
                </td>
            </tr>
            <tr>
                <td class="nametd">Work Type:</td>
                <td>
                    <select name="worktype" id="worktype" style="width:120px;">
                        <option value="1">WP
                        <option value="2">SP
                        <option value="3">EP
                        <option value="0">Other   
                    </select>
                </td>
            </tr>
            <tr><td class="nametd">Salesman:</td>
                <td>
                    <select name="saleser" id="saleser" style="width:140px;">
                        <option value="0">-Saleser-
                        <?php foreach ($marketing as $key => $value) {?>
                            <option value="<?=$value['id']?>"><?=$value['username']?>
                        <?php }?>
                    </select>
                </td>
            </tr>
            <tr><td class="nametd">Comments:</td>
                <td>
                    <textarea name="comment"><?=$order['comment']?></textarea>
                </td>
            </tr>
            <tr><td class="nametd">Create Time:</td>
                <td>
                    <?=date("d-m-Y H:i:s",$order['createtime'])?>
                </td>
            </tr>
            <tr><td class="nametd">Update Time:</td>
                <td>
                    <?=date("d-m-Y H:i:s",$order['updatetime'])?>
                </td>
            </tr>
            <tr><td class="nametd">Salesman:</td>
                <td>
                    <?=$order['employeename']?>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Save"/>
                </td>
            </tr>
        </table>
        </form>
    </div>
</div>
<script>
$("#gender").val(<?=$order['gender']?>);
$("#priority").val(<?=$order['priority']?>);
$("#worktype").val(<?=$order['worktype']?>);
$("#saleser").val(<?=$order['saleser']?>);
    
function checkEndTime(){  
    var startTime=$("input[name='s_time']").val();  
    var endTime=$("input[name='e_time']").val();
    if(startTime != '' && endTime != ''){
        var start=new Date(startTime.replace("-", "/").replace("-", "/")); 
        var end=new Date(endTime.replace("-", "/").replace("-", "/"));
        if(end<start){  
            return false;  
        }  
        return true;
    }else{
        return true;
    } 
}

$('#s_time').datetimepicker({
//    lang:"ch",           //语言选择中文
    format:"Y-m-d",      //格式化日期
    timepicker:false,    //关闭时间选项
//    yearStart：2000,     //设置最小年份
//    yearEnd:2050,        //设置最大年份
//    todayButton:false    //关闭选择今天按钮
});
$('#e_time').datetimepicker({
    format:"Y-m-d",      //格式化日期
    timepicker:false,    //关闭时间选项
});
var sns = new Array();
<?php foreach ($orders as $key => $value) { ?>
    sns[<?=$key?>] = '<?=$value['sn']?>';
<?php } ?>

$(function () {
//    $("input[name='name']").autocompleter({ 
//        source: data,
//        highlightMatches: true,
//        template: '{{ label }}',
//        hint: true,
//        empty: false,
//        callback: function (value, index, selected) {
//            if (selected) {
////                $("input[name='companysn']").val(selected.id);
//            }
//        }
//    });
});

function checkform(){
    var flag = true;
    if($.trim($("input[name='sn']").val()) == ''){
        $("#snmsg").html("SN name can not be empty!");
        flag = false;
    }else{
        $("#snmsg").html("*");
    }
    
//    var companyname = $.trim($("input[name='name']").val());
//    if(companyname == ''){
//        $("#companymsg").html("The company name can not be empty!");
//        flag = false;
//    }else{
//        $("#companymsg").html("*");
//    }
    
    if($.trim($("input[name='position']").val()) == ''){
        $("#positionmsg").html("Position can not be empty!");
        flag = false;
    }else{
        $("#positionmsg").html("*");
    }
    
    if($.trim($("input[name='salary']").val()) == ''){
        $("#salarymsg").html("Salary can not be empty!");
        flag = false;
    }else{
        $("#salarymsg").html("*");
    }
    
//    if($.trim($("input[name='s_time']").val()) == ''){
//        $("#timemsg").html("Start time can not be empty!");
//        flag = false;
//    }else if($.trim($("input[name='e_time']").val()) == ''){
//        $("#timemsg").html("End time can not be empty!");
//        flag = false;
//    }else{
//        $("#timemsg").html("*");
//    }
    
    if(!flag){
        return flag;
    }
    
//    if(!checkEndTime()){
//        $("#timemsg").html("Start time can not greater than end time!");
//        return false;
//    }else{
//        $("#timemsg").html("");
//    }
    
    var snflag = false;
    for(var i = 0;i < sns.length;i++){
        if(sns[i] == $.trim($("input[name='sn']").val()) && sns[i] != '<?=$order['sn']?>'){
            snflag = true;
        }
    }
    if(snflag){
        $("#snmsg").html("SN already exists!");
        return false;
    }else{
        $("#snmsg").html("*");
    }
    
//    var companyflag = false;
//    for(var i = 0;i < data.length;i++){
//        if(data[i]['label'] == companyname){
//            $("input[name='companysn']").val(data[i]['id']);
//            companyflag = true;
//        }
//    }
//    if(!companyflag){
//        $("input[name='companysn']").val('');
//        $("#companymsg").html("This company don't exist!");
//        return false;
//    }
    
    return flag;
}
var iTop = (document.body.clientHeight-30-600)/2; //获得窗口的垂直位置;
var iLeft = (document.body.clientWidth-10-800)/2; //获得窗口的水平位置;
$("input[name='workers']").click(function(){
    window.open('<?=base_url("profile/choice_workers/update")?>','_blank',
    'width=800,height=600,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,top='+iTop+',left='+iLeft); 
});

$("input[value='Reset']").click(function(){
    var id = $(this).attr("orderid");
    location.href = '<?=base_url("profile/setNULLByOrderID")?>/'+id;
});

$("input[name='name']").click(function(){
    window.open('<?=base_url("profile/choice_company")?>','_blank',
    'width=800,height=600,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,top='+iTop+',left='+iLeft); 
});
</script>
　